package com.wanhe.vip.module.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.wanhe.vip.R;
import com.wanhe.vip.api.bean.VipCoupon;

import java.util.ArrayList;

/**
 * Created by XieZaiWei on 2017/4/19.
 * 会员优惠券Adapter
 */

public class VipCouponAdapter extends BaseAdapter {
	private LayoutInflater mInflater;
	private ArrayList<VipCoupon> mArrVipCoupon;


	public VipCouponAdapter(Context context) {
		mInflater = LayoutInflater.from(context);
	}

	public void setData(ArrayList<VipCoupon> arrVipCoupon) {
		mArrVipCoupon = arrVipCoupon;
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		if (mArrVipCoupon != null) {
			return mArrVipCoupon.size();
		} else {
			return 0;
		}

	}

	@Override
	public VipCoupon getItem(int position) {
		if (mArrVipCoupon != null) {
			return mArrVipCoupon.get(position);
		} else {
			return null;
		}
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		ViewHolder viewHolder;
		if (convertView == null) {
			viewHolder = new ViewHolder();
			convertView = mInflater.inflate(R.layout.vip_coupon_item, null);
			viewHolder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
			viewHolder.iv_icon = (ImageView) convertView.findViewById(R.id.iv_icon);
			convertView.setTag(viewHolder);

		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}

		viewHolder.tv_name.setText(mArrVipCoupon.get(position).getName());
		if (mArrVipCoupon.get(position).isSelect()) {
			viewHolder.iv_icon.setVisibility(View.VISIBLE);
		} else {
			viewHolder.iv_icon.setVisibility(View.INVISIBLE);
		}

		return convertView;
	}

	private class ViewHolder {
		public TextView tv_name;
		public ImageView iv_icon;
	}
}
